﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sand_game_core
{
    public class Sand : particle
    {
        public Sand(uint x, uint y, int startLight)
            : base(new Microsoft.Xna.Framework.Color(176 + random.rnd.Next(60), 170 + random.rnd.Next(60), 70 + random.rnd.Next(60)), startLight)
        {
            base.x = x;
            base.y = y;
        }
        public override void update()
        {
             ammount = (uint)random.rnd.Next(2);
                upParticle = world.arrayheight > y + 1 ? world.worldParticleLayer[x * world.arraywidth + y + 1] : null;
                downParticle = y >= 1 ? world.worldParticleLayer[x * world.arraywidth + y - 1] : null;
                RightParticle = x >= 1 ? world.worldParticleLayer[(x - ammount) * world.arraywidth + y] : null;
                LeftParticle = world.arraywidth > y + 1 ? world.worldParticleLayer[(x + ammount) * world.arraywidth + y] : null;
           if (downParticle == null || downParticle is Water)
            {
                world.swap(x, y, x, y - 1);
            }
            else if (downParticle is Sand)
            {
                if (random.rnd.Next(50) == 1 && !(LeftParticle is Sand))
                {
                    if ((random.rnd.Next(0, 100) < 50))
                    {
                        world.swap(x, y, x - 1, y);
                    }
                    else if (!(random.rnd.Next(0, 100) < 50))
                    {
                        world.swap(x, y, x + 1, y);
                    }
                }
            }
            else if (LeftParticle is Obsidian)
            {
                LeftParticle.toTransition = new Ash(LeftParticle.x, LeftParticle.y, light);
                this.toTransition = new Ash(x, y, light);
            }
            else if (RightParticle is Obsidian)
            {
                RightParticle.toTransition = new Ash(RightParticle.x, RightParticle.y, light);
                this.toTransition = new Ash(x, y, light);
            }

            else if (upParticle is Obsidian)
            {
                upParticle.toTransition = new Ash(upParticle.x, upParticle.y, light);
                this.toTransition = new Ash(x, y, light);
            }
            else if (downParticle is Obsidian)
            {
                downParticle.toTransition = new Ash(downParticle.x, downParticle.y, light);
                this.toTransition = new Ash(x, y, light);
            }

            else
            {
                active = false;
            }
        }
        public override void lightMe()
        {
                light -= light >= 2 ? 2 : 0;
        }
        public override void reactivate()
        {
            if (x + 1 < world.arraywidth && y + 1 < world.arrayheight && x - 1 < world.arraywidth && y - 1 < world.arrayheight && (world.worldParticleLayer[(x + 1) * world.arraywidth + y] == null || world.worldParticleLayer[(x - 1) * world.arraywidth + y] == null || world.worldParticleLayer[x * world.arraywidth + y + 1] == null || world.worldParticleLayer[x * world.arraywidth + y - 1] == null))
            {
                active = true;
            }
        }
    }
}